package rexsee.core.utilities;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import android.database.Cursor;

public class Json {

	public static String toJson(boolean[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += (array[i]) ? "true" : "false";
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(byte[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += array[i];
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(char[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += "\"" + array[i] + "\"";
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(CharSequence[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += "\"" + String.valueOf(array[i]) + "\"";
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(Cursor cursor) {
		if (cursor == null || cursor.getColumnCount() == 0 || cursor.getCount() == 0) return "[]";
		String[] columns = new String[cursor.getColumnCount()];
		for (int i = 0; i < columns.length; i++) {
			columns[i] = cursor.getColumnName(i);
		}
		String rtn = "";
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			if (i > 0) rtn += ",";
			rtn += "{";
			for (int j = 0; j < columns.length; j++) {
				if (j > 0) rtn += ",";
				rtn += "\"" + columns[j] + "\":\"" + cursor.getString(j) + "\"";
			}
			rtn += "}";
		}
		rtn = "[" + rtn + "]";
		return rtn;
	}

	public static String toJson(Cursor cursor, int columnIndex) {
		if (cursor == null || cursor.getColumnCount() == 0 || cursor.getCount() == 0) return "[]";
		String rtn = "";
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			if (i > 0) rtn += ",";
			rtn += "\"" + cursor.getString(columnIndex) + "\"";
		}
		rtn = "[" + rtn + "]";
		return rtn;
	}

	public static String toJson(double[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += String.valueOf(array[i]);
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(float[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += String.valueOf(array[i]);
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(int[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += String.valueOf(array[i]);
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(Iterator<?> iterator) {
		if (iterator == null) return "[]";
		String rtn = "";
		while (iterator.hasNext()) {
			if (!rtn.equals("")) rtn += ",";
			rtn += "\"" + String.valueOf(iterator.next()) + "\"";
		}
		return "[" + rtn + "]";
	}

	public static String toJson(List<?> list) {
		if (list == null) return "[]";
		return toJson(list.iterator());
	}

	public static String toJson(long[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += String.valueOf(array[i]);
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(Set<?> set) {
		if (set == null) return "[]";
		return toJson(set.iterator());
	}

	public static String toJson(short[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += String.valueOf(array[i]);
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJson(String[] array) {
		if (array == null) {
			return "[]";
		} else {
			String rtn = "";
			for (int i = 0; i < array.length; i++) {
				if (i > 0) rtn += ",";
				rtn += "\"" + array[i] + "\"";
			}
			return "[" + rtn + "]";
		}
	}

	public static String toJsonColumns(Cursor cursor) {
		if (cursor == null || cursor.getColumnCount() == 0) return "[]";
		String rtn = "";
		for (int i = 0; i < cursor.getColumnCount(); i++) {
			if (i > 0) rtn += ",";
			rtn += "\"" + cursor.getColumnName(i) + "\"";
		}
		rtn = "[" + rtn + "]";
		return rtn;
	}

	public static String toJson(HashMap<String, String> map) {
		Set<String> set = map.keySet();
		Iterator<String> iterator = set.iterator();
		String rtn = "";
		while (iterator.hasNext()) {
			String key = iterator.next();
			String value = map.get(key);
			if (!rtn.equals("")) rtn += ",";
			rtn += "\"" + key + "\":\"" + value + "\"";
		}
		return "{" + rtn + "}";
	}

}
